using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.ComponentModel;

namespace TanashCMS.Business
{
    public enum ComponentType
    {
        HtmlContent,
        WebPageList,
        CategoryList,
        ProductList,
        CategoryView,
        ProductView,
        Custom
    }

    [DataObject(true)]
    public class SmartPageComponent : CMSDataObject
    {
        /*[System.Xml.Serialization.XmlIgnore]
        private TanashWebsite mWebsite = null;
        [System.Xml.Serialization.XmlIgnore]
        private WebPage mWebPage = null;*/

        private int mWebsiteID = 0;
        private int mWebPageID = 0;
        private int mSlotIndex = 0;

        private ComponentType mComponentType;
        //private string mHtmlContent = "";
        private CMSObjectType mSourceObjectType;
        private int mSourceObjectID;
        private string mUserControlName;

        //public virtual TanashWebsite Website { get { return mWebsite; } set { mWebsite = value; } }
        //public virtual WebPage WebPage { get { return mWebPage; } set { mWebPage = value; } }
        public virtual int WebsiteID { get { return mWebsiteID; } set { mWebsiteID = value; } }
        public virtual int WebPageID { get { return mWebPageID; } set { mWebPageID = value; } }
        public virtual int SlotIndex { get { return mSlotIndex; } set { mSlotIndex = value; } }
        public virtual string HtmlContent { get { return mHtmlContent; } set { mHtmlContent = value; } }
        public virtual CMSObjectType SourceObjectType { get { return mSourceObjectType; } set { mSourceObjectType = value; } }
        public virtual int SourceObjectID { get { return mSourceObjectID; } set { mSourceObjectID = value; } }
        public virtual ComponentType ComponentType { get { return mComponentType; } set { mComponentType = value; } }
        public virtual string UserControlName { get { return mUserControlName; } set { mUserControlName = value; } }

        public SmartPageComponent()
        {
            this.mObjectType = CMSObjectType.WebPage;
        }

        public static int NextIndex(int WebPageID)
        {
            return (int)Configuration.GetCurrentSession().CreateQuery("select max(c.SlotIndex) from SmartPageComponent c where c.WebPageID = " + WebPageID.ToString()).UniqueResult();
        }

        public static SmartPageComponent Select(int ID)
        {
            return Configuration.GetCurrentSession().Get<SmartPageComponent>(ID);
        }

        public static List<SmartPageComponent> List(int WebPageID)
        {
            ISession session = Configuration.GetCurrentSession();
            List<SmartPageComponent> components = (List<SmartPageComponent>)session.CreateQuery("select spc from SmartPageComponent spc where spc.WebPageID = " + WebPageID.ToString()).List<SmartPageComponent>();
            session.Disconnect();

            return components;
        }

        public static void Update(SmartPageComponent Component)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                if (Component.WebsiteID == 0 || Component.WebPageID == 0)
                {
                    SmartPageComponent originalComponent = session.Get<SmartPageComponent>(Component.ID);
                    Component.WebsiteID = originalComponent.WebsiteID;
                    Component.WebPageID = originalComponent.WebPageID;
                    session.Evict(originalComponent);
                }

                session.SaveOrUpdate(Component);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Insert(SmartPageComponent Component)
        {
            Configuration.GetCurrentSession().Save(Component);
        }

        public static void Delete(SmartPageComponent Component)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                if (session.Contains(Component))
                    session.Evict(Component);

                session.Delete(Component);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }
    }
}
